<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>使用命令行调试 python | 飞翔的竹子</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="转自：https:&#x2F;&#x2F;blog.csdn.net&#x2F;tuzixini&#x2F;article&#x2F;details&#x2F;79614655 写代码(bug)的过程中调试时不可缺少的一个环节,今天学习了Python的命令行调试工具-pdb,本篇博客为学习记录.pdb是Python自带的一个库,所以不需要另外的安装即可直接使用. (一) 进入pdb调试模式的两种方法: 直接在命令行参数指定使用pdb模块启动Python文件">
<meta property="og:type" content="article">
<meta property="og:title" content="使用命令行调试 python">
<meta property="og:url" content="http://z2a.gitee.io/2023/02/12/%E4%BD%BF%E7%94%A8%E5%91%BD%E4%BB%A4%E8%A1%8C%E8%B0%83%E8%AF%95-python/index.html">
<meta property="og:site_name" content="飞翔的竹子">
<meta property="og:description" content="转自：https:&#x2F;&#x2F;blog.csdn.net&#x2F;tuzixini&#x2F;article&#x2F;details&#x2F;79614655 写代码(bug)的过程中调试时不可缺少的一个环节,今天学习了Python的命令行调试工具-pdb,本篇博客为学习记录.pdb是Python自带的一个库,所以不需要另外的安装即可直接使用. (一) 进入pdb调试模式的两种方法: 直接在命令行参数指定使用pdb模块启动Python文件">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://img-blog.csdn.net/20180319171837504?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdHV6aXhpbmk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast">
<meta property="og:image" content="https://img-blog.csdn.net/20180319182502986?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdHV6aXhpbmk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast">
<meta property="og:image" content="https://img-blog.csdn.net/20180319182916127?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdHV6aXhpbmk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast">
<meta property="og:image" content="https://img-blog.csdn.net/20180319183222264?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdHV6aXhpbmk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast">
<meta property="og:image" content="https://img-blog.csdn.net/20180319183520836?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdHV6aXhpbmk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast">
<meta property="article:published_time" content="2023-02-12T08:48:00.000Z">
<meta property="article:modified_time" content="2023-02-19T01:15:52.523Z">
<meta property="article:author" content="flybamboo">
<meta property="article:tag" content="python">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://img-blog.csdn.net/20180319171837504?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdHV6aXhpbmk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast">
  
    <link rel="alternate" href="/atom.xml" title="飞翔的竹子" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/fancybox/jquery.fancybox.min.css">

  
<meta name="generator" content="Hexo 4.2.1"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">飞翔的竹子</a>
      </h1>
      
        <h2 id="subtitle-wrap">
          <a href="/" id="subtitle">凤凰涅槃，死而后生，翱翔九天，十日而亡</a>
        </h2>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://z2a.gitee.io"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-使用命令行调试-python" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/2023/02/12/%E4%BD%BF%E7%94%A8%E5%91%BD%E4%BB%A4%E8%A1%8C%E8%B0%83%E8%AF%95-python/" class="article-date">
  <time class="dt-published" datetime="2023-02-12T08:48:00.000Z" itemprop="datePublished">2023-02-12</time>
</a>
    
  <div class="article-category">
    <a class="article-category-link" href="/categories/%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/">开发工具</a>
  </div>

  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      使用命令行调试 python
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <p>转自：<a href="https://blog.csdn.net/tuzixini/article/details/79614655" target="_blank" rel="noopener">https://blog.csdn.net/tuzixini/article/details/79614655</a></p>
<p>写代码(bug)的过程中调试时不可缺少的一个环节,今天学习了Python的命令行调试工具-pdb,本篇博客为学习记录.<br>pdb是Python自带的一个库,所以不需要另外的安装即可直接使用.</p>
<h2 id="一-进入pdb调试模式的两种方法"><a href="#一-进入pdb调试模式的两种方法" class="headerlink" title="(一) 进入pdb调试模式的两种方法:"></a>(一) 进入pdb调试模式的两种方法:</h2><ol>
<li><p>直接在命令行参数指定使用pdb模块启动Python文件<br><code>python -m pdb test.py</code><br>程序将进入调试界面<br>这里写图片描述<br><img src="https://img-blog.csdn.net/20180319171837504?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdHV6aXhpbmk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt=""></p>
</li>
<li><p>在程序代码中设置断点<br>首先需要在程序中使用如下语句导入pdb模块:<br><code>import pdb</code><br>然后再在需要设置断点的地方放置如下断点设置代码:<br><code>pdb.set_trace()</code><br>然后在命令行中正常运行程序,程序会在设置断点的位置自动停下进入pdb调试模式.<br>例如运行如下程序代码:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">#&#x2F;usr&#x2F;bin&#x2F;python</span><br><span class="line">import pdb</span><br><span class="line"></span><br><span class="line">for i in range(1,10):</span><br><span class="line">    pdb.set_trace()</span><br><span class="line">    print(i)</span><br></pre></td></tr></table></figure>

</li>
</ol>
<p>这里写图片描述<br><img src="https://img-blog.csdn.net/20180319182502986?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdHV6aXhpbmk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt=""><br>发现程序停在了指定位置(这里是第6行),并且进入了调试模式.</p>
<h2 id="二-调试程序的方法"><a href="#二-调试程序的方法" class="headerlink" title="(二) 调试程序的方法"></a>(二) 调试程序的方法</h2><p>进入调试程序后可以使用相应的命令进行指定的操作.<br>这里介绍几个常用的命令:可以使用命令全称也可以使用缩写</p>
<ol>
<li><p>使用pdb的命令<br>next(缩写:n),向下执行一步,<br><img src="https://img-blog.csdn.net/20180319182916127?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdHV6aXhpbmk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt=""><br>比如我这里还是刚才那个程序,输入n回车之后,程序执行了进入断点处的下一句代码(也就是print(i))<br>更多的命令请看-&gt;4.命令附录</p>
</li>
<li><p>直接输入变量名称<br>还可以直接输入变量的名称查看变量的值<br><img src="https://img-blog.csdn.net/20180319183222264?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdHV6aXhpbmk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt=""></p>
</li>
<li><p>使用类的方法查看变量/类的信息(输入表达式)<br>像数组这种还可以使用类似 i.shape 这样的输入直接调用相关方法,这个拓展就很广了,大家可以自由发挥<br><img src="https://img-blog.csdn.net/20180319183520836?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdHV6aXhpbmk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt=""><br>当然我这里的i是个int肯定是无法查看shape的,但是可以看见pdb会详细给出相应的报错提示信息,还是很好用的.</p>
</li>
<li><p>更多的命令以及详细用法</p>
<table>
<thead>
<tr>
<th align="left">命令</th>
<th align="left">缩写</th>
<th align="left">说明</th>
</tr>
</thead>
<tbody><tr>
<td align="left">break</td>
<td align="left">b</td>
<td align="left">设置断点</td>
</tr>
<tr>
<td align="left">continue</td>
<td align="left">cont/c</td>
<td align="left">继续执行至下一个断点</td>
</tr>
<tr>
<td align="left">next</td>
<td align="left">n</td>
<td align="left">执行下一行,如果下一行是子程序,不会进入子程序</td>
</tr>
<tr>
<td align="left">step</td>
<td align="left">s</td>
<td align="left">执行下一行,如果下一行是子程序,会进如子程序</td>
</tr>
<tr>
<td align="left">where</td>
<td align="left">bt/w</td>
<td align="left">打印堆栈轨迹</td>
</tr>
<tr>
<td align="left">enable</td>
<td align="left">-</td>
<td align="left">启用禁用的断点</td>
</tr>
<tr>
<td align="left">disable</td>
<td align="left">-</td>
<td align="left">禁用启用的断点</td>
</tr>
<tr>
<td align="left">pp/p</td>
<td align="left">-</td>
<td align="left">打印变量或表达式</td>
</tr>
<tr>
<td align="left">list</td>
<td align="left">l</td>
<td align="left">根据参数值打印源码</td>
</tr>
<tr>
<td align="left">up</td>
<td align="left">u</td>
<td align="left">移动到上一层堆栈</td>
</tr>
<tr>
<td align="left">down</td>
<td align="left">d</td>
<td align="left">移动到下一层堆栈</td>
</tr>
<tr>
<td align="left">restart</td>
<td align="left">run</td>
<td align="left">重新开始调试</td>
</tr>
<tr>
<td align="left">args</td>
<td align="left">a</td>
<td align="left">打印函数参数</td>
</tr>
<tr>
<td align="left">clear</td>
<td align="left">cl</td>
<td align="left">清除所有断点</td>
</tr>
<tr>
<td align="left">return</td>
<td align="left">r</td>
<td align="left">执行到当前函数结束</td>
</tr>
<tr>
<td align="left">quit</td>
<td align="left">q</td>
<td align="left">结束调试,退出当前程序</td>
</tr>
</tbody></table>
</li>
</ol>
<blockquote>
<p>注:需要注意n和s两个命令的区别,restart和run这里写作缩写,其实这两个命令可以看成是同一个命令.</p>
</blockquote>
<p>这里只列出了部分比较常用的命令,更多的命令以及详细的解释请参考Python官方文档:<br><a href="https://docs.python.org/3/library/pdb.html" target="_blank" rel="noopener">https://docs.python.org/3/library/pdb.html</a></p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://z2a.gitee.io/2023/02/12/%E4%BD%BF%E7%94%A8%E5%91%BD%E4%BB%A4%E8%A1%8C%E8%B0%83%E8%AF%95-python/" data-id="cleao6htc000pusfye97s70cg" data-title="使用命令行调试 python" class="article-share-link">Share</a>
      
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/python/" rel="tag">python</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2023/02/14/Markdown-notes/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          Markdown notes
        
      </div>
    </a>
  
  
    <a href="/2023/02/12/setupHexoEnv/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">使用 Hexo 和 gitee 搭建个人博客</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Categories</h3>
    <div class="widget">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/categories/%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/">开发工具</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/tags/Hexo/" rel="tag">Hexo</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/blog/" rel="tag">blog</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/markdown/" rel="tag">markdown</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/node/" rel="tag">node</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/npm/" rel="tag">npm</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/python/" rel="tag">python</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/tags/Hexo/" style="font-size: 10px;">Hexo</a> <a href="/tags/blog/" style="font-size: 10px;">blog</a> <a href="/tags/markdown/" style="font-size: 10px;">markdown</a> <a href="/tags/node/" style="font-size: 10px;">node</a> <a href="/tags/npm/" style="font-size: 10px;">npm</a> <a href="/tags/python/" style="font-size: 10px;">python</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2023/02/">二月 2023</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2023/02/15/npm-package-version/">npm package version semver calculator</a>
          </li>
        
          <li>
            <a href="/2023/02/14/Markdown-notes/">Markdown notes</a>
          </li>
        
          <li>
            <a href="/2023/02/12/%E4%BD%BF%E7%94%A8%E5%91%BD%E4%BB%A4%E8%A1%8C%E8%B0%83%E8%AF%95-python/">使用命令行调试 python</a>
          </li>
        
          <li>
            <a href="/2023/02/12/setupHexoEnv/">使用 Hexo 和 gitee 搭建个人博客</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2023 flybamboo<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/js/jquery-3.4.1.min.js"></script>



  
<script src="/fancybox/jquery.fancybox.min.js"></script>




<script src="/js/script.js"></script>





  </div>
</body>
</html>